﻿using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Linq;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WorkflowActions;
using Microsoft.Office.DocumentManagement.DocumentSets;
using System.Diagnostics;

namespace QNH.ActivityLibrary
{
    public partial class GetDocumentSetProperty : SequenceActivity
    {
       
        #region Static dependency properties

        public static DependencyProperty __ContextProperty = DependencyProperty.Register(
            "__Context", typeof(WorkflowContext), typeof(GetDocumentSetProperty));

        public static DependencyProperty VariabelProperty = DependencyProperty.Register(
            "Variabel", typeof(string), typeof(GetDocumentSetProperty));

        public static DependencyProperty ColumnProperty = DependencyProperty.Register(
            "Column", typeof(string), typeof(GetDocumentSetProperty));

        #endregion

        #region Properties

        [ValidationOption(ValidationOption.Optional)]
        public WorkflowContext __Context
        {
            get
            {
                return (WorkflowContext)base.GetValue(__ContextProperty);
            }
            set
            {
                base.SetValue(__ContextProperty, value);
            }
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        [ValidationOption(ValidationOption.Required)]
        [Browsable(true)]
        public string Variabel
        {
            get { return ((string)(base.GetValue(GetDocumentSetProperty.VariabelProperty))); }
            set { base.SetValue(GetDocumentSetProperty.VariabelProperty, value); }
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        [ValidationOption(ValidationOption.Required)]
        [Browsable(true)]
        public string Column
        {
            get { return ((string)(base.GetValue(GetDocumentSetProperty.ColumnProperty))); }
            set { base.SetValue(GetDocumentSetProperty.ColumnProperty, value); }
        }

        #endregion

        public GetDocumentSetProperty()
        {
            InitializeComponent();
        }

        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            try
            {
                using (SPWeb web = this.__Context.Web)
                {
                    SPList list = web.Lists[new Guid(__Context.ListId)];
                    SPListItem listItem = list.Items.GetItemById(__Context.ItemId);
                    if (listItem.File.ParentFolder != null)
                    {
                        DocumentSet docset = DocumentSet.GetDocumentSet(listItem.File.ParentFolder);
                        SPListItem spli = docset.Item;
                        string local = (string)spli[Column];
                        SetValueOfWorkflowVariable(this.Parent, Variabel, local);
                    }
                }
            }
            catch (Exception e)
            {
                HelperMethods.ErrorHandling(e);
            }

            return ActivityExecutionStatus.Closed;
        }

        public static void SetValueOfWorkflowVariable(Activity activity, string valueName, object value)
        {
            if (activity != null)
            {
                try
                {
                    ActivityBind workflowActivityBind = new ActivityBind();
                    workflowActivityBind.Name = activity.Name;
                    workflowActivityBind.Path = valueName;
                    workflowActivityBind.SetRuntimeValue(activity, value);
                }
                catch (Exception e)
                {
                    HelperMethods.ErrorHandling(e);
                }
                SetValueOfWorkflowVariable(activity.Parent, valueName, value);
            }
        }
    }
}
